

#include <GL/glew.h>
#include <GLFW/glfw3.h>

#include "include/GUI.h"
//#include "Engine/include/main_engine.h"

using namespace std;
using namespace GUI;
//using namespace ECardinal;

GLFWwindow* m_window;

Window::Window() {

}

bool Window::Init(int width, int height, string name, int major, int minor) {
    if(!glfwInit()) {
        cout << "glfwInit() failed" << endl;
        return false;
    }

    glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, major);
    glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, minor);
    glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);

    m_window = glfwCreateWindow(width, height, name.c_str(), NULL, NULL);

    if (!m_window) {
        cout << "Failed to create window" << endl;
        glfwTerminate(); // terminate
        return false;
    }

    glfwMakeContextCurrent(m_window);

    glewExperimental = true;
    if (glewInit() != GLEW_OK) {
        cout << "Failed to init GLEW" << endl;
        glfwTerminate();
        return false;
    }

    return true;
}


Window::Window(int width, int height, string name, int major, int minor) {
    Init(width, height, name, major, minor);
}

Window::~Window() {
    glfwTerminate(); // terminate
}

void Window::Update(void(*function)()) {
    while (!glfwWindowShouldClose(m_window)) {
        function();

        glfwSwapBuffers(m_window);
        glfwPollEvents(); // update
    }
}



